﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommerceOne.Domains.Commons;

namespace CommerceOne.Widgets.FilterNavigation
{
    public class NavigationSelectedItem
    {
        public FilterItem ItemType { get; set; }
        public int FilterTypeId { get; set; }
        public List<Object> Value { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationSelectedItem"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="value">The value.</param>
        public NavigationSelectedItem(string type, string value)
        {
            this.Value = new List<object>();
            string[] paramVal = value.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            Value.AddRange(paramVal);

            switch (type)
            {
                case "P":
                    ItemType = FilterItem.Price;
                    break;

                case "B":
                    ItemType = FilterItem.Brand;
                    break;

                case "C":
                    ItemType = FilterItem.Category;
                    break;

                default:
                    ItemType = FilterItem.Generic;
                    int typeId = 0;
                    if (int.TryParse(type, out typeId)) FilterTypeId = typeId;
                    break;
            }
        }

        public List<IFilterParam> MakeFilterParam()
        {
            List<IFilterParam> parameters = new List<IFilterParam>();

            switch (ItemType)
            {
                case FilterItem.Brand:
                    IFilterParam brandParam = new FilterParam<int>("ProductSearchView", "BrandId", FilterType.Equal, Convert.ToInt32(Value[0]));
                    parameters.Add(brandParam);
                    break;

                case FilterItem.Category:
                    IFilterParam categoryParam = new FilterParam<int>("ProductSearchView", "CategoryId", FilterType.Equal, Convert.ToInt32(Value[0]));
                    parameters.Add(categoryParam);
                    break;

                case FilterItem.Price:
                    IFilterParam priceParam = new PriceParam<decimal>();
                    priceParam.FieldName = "ProductSearchView";
                    if (Value.Count >= 1) priceParam.AddValue(Convert.ToDecimal(Value[0]));
                    if (Value.Count >= 2) priceParam.AddValue(Convert.ToDecimal(Value[1]));
                    parameters.Add(priceParam);
                    break;

                case FilterItem.Generic:
                    // TODO: for generic item
                    break;

                default:
                    break;
            }

            return parameters;
        }

        /// <summary>
        /// Res the make query string.
        /// </summary>
        /// <returns></returns>
        public string ReMakeQueryString()
        {
            string value = string.Join("_", this.Value);
            string type = string.Empty;
            switch (ItemType)
            {
                case FilterItem.Brand:
                    type = "B";
                    break;
                case FilterItem.Category:
                    type = "C";
                    break;
                case FilterItem.Price:
                    type = "P";
                    break;
                case FilterItem.Generic:
                    break;
                default:
                    break;
            }
            return type + ":" + value;
        }

        /// <summary>
        /// Makes the query string.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="param">The param.</param>
        /// <returns></returns>
        public static string MakeQueryString(FilterItem item, params object [] param)
        {
            if (param == null || param.Count() == 0) return string.Empty;

            string value = string.Join("_", param);
            string type = string.Empty;
            switch (item)
            {
                case FilterItem.Brand:
                    type = "B";
                    break;
                case FilterItem.Category:
                    type = "C";
                    break;
                case FilterItem.Price:
                    type = "P";
                    break;
                case FilterItem.Generic:
                    break;
                default:
                    break;
            }
            return type + ":" + value;
        }
    }
}